package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.entity.ShopGoods;
import com.kgc.scd.mapper.ShopGoodsMapper;
import com.kgc.scd.service.ShopGoodsServerService;
import com.kgc.scd.vo.ShopGoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created On : 2022/11/17.
 * <p>
 * Author : Administrator
 * <p>
 * Description: ShopGoodsServerServiceImpl
 */
@Slf4j
@Service
public class ShopGoodsServerServiceImpl implements ShopGoodsServerService {

    @Autowired(required = false)
    private ShopGoodsMapper shopGoodsMapper;

    @Override
    public ShopGoodsVO getShopGoodsById(String pid) {

        ShopGoods shopGoods=shopGoodsMapper.selectById(pid);

        ShopGoodsVO shopGoodsVO=new ShopGoodsVO();
        BeanUtils.copyProperties(shopGoods,shopGoodsVO);
        shopGoodsVO.setGoodsNumber(shopGoods.getGoodsNumber().toString());

        return shopGoodsVO;
    }

    @Override
    public void updateShopGoodsSubSellVolumeById(ShopGoodsVO shopGoodsVO, int number) {

        log.info("impl:"+shopGoodsVO);
        ShopGoods shopGoods=new ShopGoods();
        BeanUtils.copyProperties(shopGoodsVO,shopGoods);
        log.info("impl:"+shopGoods);
        shopGoods.setGoodsNumber(Integer.valueOf(shopGoodsVO.getGoodsNumber()));

        log.info("shopGoodsImpl:"+shopGoods);
        log.info("impl:"+shopGoods.getGoodsNumber());
        shopGoods.setGoodsNumber(shopGoods.getGoodsNumber()-number);
        shopGoods.setSellVolume(shopGoods.getSellVolume()+number);

        QueryWrapper<ShopGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",shopGoods.getId());

        shopGoodsMapper.update(shopGoods,queryWrapper);
    }

    @Override
    public boolean judegeGoodNumberEnough(String pid, Integer number) {

        ShopGoods shopGoods=shopGoodsMapper.selectById(pid);


        return shopGoods.getGoodsNumber() - number > 0;

    }
}
